package com.cake.service.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cake.base.config.Constant;
import com.cake.base.util.Base64FileUtils;
import com.cake.base.util.PropertiesUtils;
import com.cake.base.util.ReturnUtils;
import com.cake.dao.XxBannerImageMapper;
import com.cake.dao.XxBannerMapper;
import com.cake.dao.XxProductCategoryMapper;
import com.cake.dao.XxProductMapper;
import com.cake.entity.XxBanner;
import com.cake.entity.XxBannerImage;
import com.cake.entity.XxProduct;
import com.cake.entity.XxProductCategory;
import com.cake.service.base.BaseService;
import com.cake.service.inter.IXxBannerService;

/**
 *	banner业务层
 * @author Administrator
 *
 */
@Service
@Transactional
public class XxBannerServiceImpl extends BaseService<XxBanner> implements IXxBannerService{

	@Autowired
	private XxBannerMapper xxBannerMapper;
	
	@Autowired
	private XxBannerImageMapper xxBannerImageMapper;
	
	@Autowired
	private XxProductMapper xxProductMapper;
	
	@Autowired
	private XxProductCategoryMapper xxProductCategoryMapper;
	
	@Override
	public ReturnUtils selectBanners() {
		List<XxBanner> list = xxBannerMapper.selectBanners();
		for (XxBanner xxBanner : list) {
			xxBanner.setBannerImages(xxBannerImageMapper.selectItemsByBannerId(xxBanner.getId()));
		}
		return ReturnUtils.forNoramlSuccess(list);
	}

	@Override
	public ReturnUtils selectBannerById(Long bannerId) {
		XxBanner xxBanner = xxBannerMapper.selectByPrimaryKey(bannerId);
		if(xxBanner != null){
			xxBanner.setBannerImages(xxBannerImageMapper.selectItemsByBannerId(xxBanner.getId()));
		}
		xxBanner.setLinkUrlPre(PropertiesUtils.getProperties(Constant.SERVER_WEBSITE_URL_PRE));
		return ReturnUtils.forNoramlSuccess(xxBanner);
	}

	@Override
	public ReturnUtils updateBannerAndBannerImage(XxBanner xxBanner) throws Exception {
		int updateCode = xxBannerMapper.updateByPrimaryKeySelective(xxBanner);
		if(updateCode > 0){
			xxBannerImageMapper.deleteBannerImagesByBannerId(xxBanner.getId());
			List<XxBannerImage> bannerImages = xxBanner.getBannerImages();
			for (XxBannerImage xxBannerImage : bannerImages) {
				String imageUrl = Base64FileUtils.base64ToImageFile(xxBannerImage.getBannerImageStr(), 
						PropertiesUtils.getProperties(Constant.SERVER_FILE_IMAGE_PHYSICAL_URL_PRE),
						UUID.randomUUID().toString() + ".png");
				xxBannerImage.setBanner(xxBanner.getId());
				xxBannerImage.setThumbnail(imageUrl);
				xxBannerImage.setSource(imageUrl);
				xxBannerImageMapper.insertSelective(xxBannerImage);
			}
		}
		return ReturnUtils.forNoraml("000000", "操作成功");
	}

	@Override
	public List<Map<String, Object>> selectProductBasicInfoForBanner() throws Exception {
		List<XxProduct> list = xxProductMapper.selectByAttributeAnd(new XxProduct());
		List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
		for (XxProduct xxProduct : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", xxProduct.getId());
			map.put("name", xxProduct.getName());
			map.put("linkUrlPostfix", "getProduct?productId=");
			returnList.add(map);
		}
		return returnList;
	}

	@Override
	public List<Map<String, Object>> selectProductCategoryBasicInfoForBanner() throws Exception {
		List<XxProductCategory> list = xxProductCategoryMapper.selectByAttributeAnd(new XxProductCategory());
		List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
		for (XxProductCategory xxProductCategory : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", xxProductCategory.getId());
			map.put("name", xxProductCategory.getName());
			map.put("linkUrlPostfix", "findProductByCategoryId?categoryId=");
			returnList.add(map);
		}
		return returnList;
	}
}
